package com.hqyj.springBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.springBoot.modules.common.vo.Result;
import com.hqyj.springBoot.modules.common.vo.Search;
import com.hqyj.springBoot.modules.account.entity.Resource;
import com.hqyj.springBoot.modules.account.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.MediaType;

@RestController
@RequestMapping("/api")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;


    /**
     * 127.0.0.1/api/resource ----post
     * {"createDate":"2022-09-11 14:57:57","permission":"insertUser"}
     */
    @PostMapping(value = "/resource",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Resource> insertResource(@RequestBody Resource resource){
        return resourceService.insertResource(resource);
    }

    /**
     * 127.0.0.1/api/resource ---put
     * {"createDate":"2022-09-11 20:00:00","permission":"updateUser","resourceId":5}
     */
    @PutMapping(value = "/resource",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Resource> updateResource(@RequestBody Resource resource){
        return resourceService.updateResource(resource);
    }

    /**
     * 127.0.0.1/api/resource/resourceId ----delete
     */
    @DeleteMapping("/resource/{resourceId}")
    public Result<Object> deleteResourceById(@PathVariable int resourceId){
        return resourceService.deleteResourceById(resourceId);
    }

    /**
     * 127.0.0.1/api/resource/resourceId ----get
     */
    @GetMapping("/resource/{resourceId}")
    public Resource selectResourceById(@PathVariable int resourceId){
        return resourceService.selectResourceById(resourceId);
    }

    /**
     * 127.0.0.1/api/resources ----post
     * {"currentPage":1,"pageSize":3,"sort":"create_date","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/resources",consumes = MediaType.APPLICATION_JSON_VALUE)
    PageInfo<Resource> getResourcesBySearch(@RequestBody Search search){
        return resourceService.getResourcesBySearch(search);
    }
}
